package com.itheima.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author: libin
 * @date: 2019/11/25 10:18
 * @version: 1.0
 * @describe:
 */
@RequestMapping("/ordersetting")
@RestController
public class OrderSettingController {
    @Reference
    private OrderSettingService orderSettingService;

    /**
     * 设置预约人数
     * @param orderSetting
     * @return
     */
    @RequestMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting) {
        try {
            orderSettingService.editNumberByDate(orderSetting);
        } catch (Exception e) {
            return new Result(true, MessageConstant.GET_ORDERSETTING_FAIL);
        }
        return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS);
    }

    //自定义非空数据判断
    public boolean isNotEmpty(Object o) {
        if (null == o) {
            return false;
        }
        if (o instanceof String) {
            String o1= (String) o;
            if (o1.equals("") || o1.equals(" ")) {
                return false;
            }
        }
        return true;
    }

    /**
     * 从xlsx文件中批量导入预约
     * @param excelFile
     * @return
     */
    @RequestMapping("/upload")
    public Result upload(MultipartFile excelFile)  {
        try {
            //使用poiUtils工具类读取文件数据
            List<String[]> data = POIUtils.readExcel(excelFile);
            List<OrderSetting> orderSettings =new ArrayList<>();
            if (CollectionUtil.isNotEmpty(data)) {
                //将data中的数据封装到OrderSetting对象
                for (String[] strings : data) {
                    if (strings.length != 2) {//数据一定要完整
                        continue;//跳过本次循环
                    }
                    String date = strings[0];//yyyy/MM/dd
                    String number = strings[1];
                    //判断2个数据不为空
                    if (isNotEmpty(date) && isNotEmpty(number)) {
                        //创建对象，封装数据
                        OrderSetting orderSetting = new OrderSetting();
                        orderSetting.setOrderDate(DateUtil.parse(date,"yyyy/MM/dd"));
                        orderSetting.setNumber(Integer.parseInt(number));
                        //将对象放入集合中
                        orderSettings.add(orderSetting);
                    }
                }
            }
            orderSettingService.upload(orderSettings);
            return new Result(true,MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            return new Result(false,MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }


    /**
     *日历数据回显
     * @param date
     * @return
     */
    @RequestMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String date) {
        try {
            //返回{"date":22,"mouth":8,"number":300,"reservations":300,}
            List<Map> maps = orderSettingService.getOrderSettingByMonth(date);
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS,maps);
        } catch (Exception e) {
            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
        }

      }
    }
